/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.utils;

import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;

/**
 * RectFloatUtil
 */
public class RectFloatUtil extends RectFloat implements RectFloatInterface {
    /**
     * left
     */
    public float left;
    /**
     * top
     */
    public float top;
    /**
     * right
     */
    public float right;
    /**
     * bottom
     */
    public float bottom;

    /**
     * RectFloatUtil
     */
    public RectFloatUtil() {
        super();
    }

    public RectFloatUtil(float left, float top, float right, float bottom) {
        super(left, top, right, bottom);
        this.left = left;
        this.bottom = bottom;
        this.right = right;
        this.top = top;
    }

    @Override
    public boolean intersect(float left, float top, float right, float bottom) {
        if (this.left < right && left < this.right && this.top < bottom && top < this.bottom) {
            if (this.left < left) {
                this.left = left;
            }
            if (this.top < top) {
                this.top = top;
            }
            if (this.right > right) {
                this.right = right;
            }
            if (this.bottom > bottom) {
                this.bottom = bottom;
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean intersect(RectFloatUtil rectFloatUtil) {
        return intersect(rectFloatUtil.left, rectFloatUtil.top, rectFloatUtil.right, rectFloatUtil.bottom);
    }

    /**
     * gives rectangle's width
     *
     * @return the rectangle's width. This does not check for a valid rectangle
     * (i.e. left <= right) so the result may be negative.
     */
    public final float width() {
        return right - left;
    }

    /**
     * gives rectangles height
     *
     * @return the rectangle's height. This does not check for a valid rectangle
     * (i.e. top <= bottom) so the result may be negative.
     */
    public final float height() {
        return bottom - top;
    }

    @Override
    public void union(float left, float top, float right, float bottom) {
        if ((left < right) && (top < bottom)) {
            if ((this.left < this.right) && (this.top < this.bottom)) {
                if (this.left > left) {
                    this.left = left;
                }
                if (this.top > top) {
                    this.top = top;
                }
                if (this.right < right) {
                    this.right = right;
                }
                if (this.bottom < bottom) {
                    this.bottom = bottom;
                }
            } else {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }
        }
    }

    @Override
    public void union(RectFloatUtil rectFloatUtil) {
        union(rectFloatUtil.left, rectFloatUtil.top, rectFloatUtil.right, rectFloatUtil.bottom);
    }

    @Override
    public void set(float left, float top, float right, float bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    /**
     * set RectFloat
     *
     * @param rectFloatUtil RectFloat
     */
    public void set(RectFloatUtil rectFloatUtil) {
        this.left = rectFloatUtil.left;
        this.top = rectFloatUtil.top;
        this.right = rectFloatUtil.right;
        this.bottom = rectFloatUtil.bottom;
    }

    /**
     * Copy the coordinates from src into this rectangle.
     *
     * @param src The rectangle whose coordinates are copied into this
     *            rectangle.
     */
    public void set(Rect src) {
        this.left = src.left;
        this.top = src.top;
        this.right = src.right;
        this.bottom = src.bottom;
    }
}
